package com.itmayiedu.controller;

import com.itmayiedu.base.ResponseBase;
import com.itmayiedu.constants.Constants;
import com.itmayiedu.feign.MemberServiceFeign;
import com.itmayiedu.utils.CookieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;

/**
 * @Author cht 【wonderfor@vip.qq.com】
 * @Date 2018/10/23
 */
@Controller
public class IndexController {

    private static final String INDEX = "index";

    @Autowired
    private MemberServiceFeign memberServiceFeign;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(HttpServletRequest request) {
        //1.从cookie获取token信息
        String token = CookieUtil.getUid(request, Constants.COOKIE_MEMBER_TOKEN);
        //2.如果cookie存在token
        if(!StringUtils.isEmpty(token)) {
            ResponseBase responseBase = memberServiceFeign.findByTokenUser(token);
            if(responseBase.getRtnCode().equals(Constants.HTTP_RES_CODE_200)) {
                LinkedHashMap userData = (LinkedHashMap) responseBase.getData();
                String userName = (String) userData.get("username");
                request.setAttribute("username", userName);
            }
        }
        return INDEX;
    }

}
